package com.ruoyi.web.controller.system;

import java.util.*;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysFileInfo;
import com.ruoyi.system.service.ISysFileInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传Controller
 * 
 * @author GuangPipi
 * @date 2023-06-11
 */
@RestController
@RequestMapping("/system/info")
public class SysFileInfoController extends BaseController {
    @Autowired
    private ISysFileInfoService sysFileInfoService;

    /**
     * 查询文件上传列表
     */
    //// @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysFileInfo sysFileInfo) {
        startPage();
        List<SysFileInfo> list = sysFileInfoService.selectSysFileInfoList(sysFileInfo);
        return getDataTable(list);
    }

    @PostMapping("/importFilePath")
    public AjaxResult importFilePath(MultipartFile file, Long engclass) {

        String message = sysFileInfoService.importFilePath(file, engclass);
        return success(message);
    }

    // @PostMapping("/importFilePath")
    // public AjaxResult importFilePath(MultipartFile file){
    //
    // String message = sysFileInfoService.importFilePath(file);
    // return success(message);
    // }

    /**
     * 导出文件上传列表
     */
    // @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "文件上传", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFileInfo sysFileInfo) {
        List<SysFileInfo> list = sysFileInfoService.selectSysFileInfoList(sysFileInfo);
        ExcelUtil<SysFileInfo> util = new ExcelUtil<SysFileInfo>(SysFileInfo.class);
        util.exportExcel(response, list, "文件上传数据");
    }

    /**
     * 获取文件上传详细信息
     */
    // @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{engFileId}")
    public AjaxResult getInfo(@PathVariable("engFileId") Long engFileId) {
        return success(sysFileInfoService.selectSysFileInfoByEngFileId(engFileId));
    }

    /**
     * 新增文件上传
     */
    // @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFileInfo sysFileInfo) {
        return toAjax(sysFileInfoService.insertSysFileInfo(sysFileInfo));
    }

    /**
     * 修改文件上传
     */
    // @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "文件上传", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFileInfo sysFileInfo) {
        return toAjax(sysFileInfoService.updateSysFileInfo(sysFileInfo));
    }

    /**
     * 删除文件上传
     */
    // @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "文件上传", businessType = BusinessType.DELETE)
    @DeleteMapping("/{engFileIds}")
    public AjaxResult remove(@PathVariable Long[] engFileIds) {
        return toAjax(sysFileInfoService.deleteSysFileInfoByEngFileIds(engFileIds));
    }

    @GetMapping("/getQuarterly")
    public Map<Object, Object> getQuarterly() {
        Map<Object, Object> returnMap = new HashMap<Object, Object>();
        List<SysFileInfo> sysFileInfoList = sysFileInfoService.getAllFileInfoList();
        // 创建Calendar实例
        Calendar calendar = Calendar.getInstance();
        // 获取当前年份
        int year = calendar.get(Calendar.YEAR);
        int[] yearnumber = new int[4];
        for (int i = 0; i < yearnumber.length; i++) {
            yearnumber[i] = 0;
        }
        int[] year1number = new int[4];
        for (int i = 0; i < year1number.length; i++) {
            year1number[i] = 0;
        }
        int year1 = year - 1;

        for (SysFileInfo sysFileInfo : sysFileInfoList) {
            Date date = sysFileInfo.getCreateTime();
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(date);
            int years = calendar1.get(Calendar.YEAR);
            if (years == year) {
                int month = calendar1.get(Calendar.MONTH) + 1;
                if (1 <= month && month <= 3) {
                    yearnumber[0]++;
                } else if (4 <= month && month <= 6) {
                    yearnumber[1]++;
                } else if (7 <= month && month <= 9) {
                    yearnumber[2]++;
                } else {
                    yearnumber[3]++;
                }
            } else if (years == year1) {
                int month = calendar1.get(Calendar.MONTH) + 1;
                if (1 <= month && month <= 3) {
                    year1number[0]++;
                } else if (4 <= month && month <= 6) {
                    year1number[1]++;
                } else if (7 <= month && month <= 9) {
                    year1number[2]++;
                } else {
                    year1number[3]++;
                }
            }
        }
        returnMap.put("当前年份", year);
        returnMap.put("往年年份", year1);
        returnMap.put("当年数量", yearnumber);
        returnMap.put("往年数量", year1number);
        return returnMap;
    }
}
